﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System;

namespace ConsoleApp
{
    internal class Event
    {
        static void Main1(string[] args)
        {
            Customer customer = new Customer();
            Waiter waiter = new Waiter();
            customer.Order += new EventHandler(waiter.Action) ;
            customer.Action();
            customer.PayBill();
        }
    }
    public class OrderEventArgs:EventArgs
    {
        public string disName { get; set; }
        public string size { get; set; } 
    }
    //定义委托类型
    //public delegate void OrderEventHandler(Customer customer, OrderEventArgs e);
    public class Customer//事件拥有者
    {
        public double Bill { get; set; }
        public void PayBill()
        {
            Console.WriteLine("I will pay the {0}",this.Bill);
        }
        //使用委托类型声明一个委托字段
        /*private OrderEventHandler orderEventHandler { get; set; }

        //声明事件
        public event OrderEventHandler Order//事件
        {
            add
            {
                this.orderEventHandler += value;
            }
            remove
            {
                this.orderEventHandler -= value;
            }
        }*/
        public event EventHandler Order;
        
        public void WalkIn()
        {
            Console.WriteLine("Im walking in");
        }
        public void SiteDown()
        {
            Console.WriteLine("Im sitedown");
        }
        
        public void Thinking()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Im thinking");
                
            }
            this.OnOrder("guobaorou", "large");

        }
        protected void OnOrder(string dishName,string size)
        {
            if (this.Order != null)
            {
                OrderEventArgs e = new OrderEventArgs();
                e.disName = dishName;
                e.size = size;
                this.Order.Invoke(this, e);
            }
        }
        public void Action()
        {
            Console.ReadLine();
            this.WalkIn();
            this.SiteDown();
            this.Thinking();

        }
    }
    public class Waiter
    {
        public void Action(object sender, EventArgs e)
        {
            Customer customer = sender as Customer;
            OrderEventArgs orderInfo = e as OrderEventArgs;
            Console.WriteLine("i will serve you {0}",orderInfo.disName);
            double price = 10;
            switch (orderInfo.size) 
            {
                case "small":
                    price *= 0.5;
                    break;
                case "large":
                    price *= 1.5; 
                    break;
                default:
                    break;
            }
            customer.Bill += price;
        }
        
    }

}
